package database;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import user.Customer;

public class CustomerTable extends Table {

	private static final String CUSTOMER_INSERT = "INSERT INTO Customer VALUES ('%s', '%s', '%s', '%s', '%s')";
	private static final String CUSTOMER_DELETE = "DELETE FROM Customer WHERE cid='%s'";
	private static final String CUSTOMER_DISPLAY = "SELECT * FROM Customer";	
	
	private static final String LOGIN_QUERY = "SELECT * FROM Customer WHERE cid = '%s' AND password = '%s'";
	private static final String EXISTS_QUERY = "SELECT * FROM Customer WHERE cid = '%s'";
		
	public enum Column
	{
		CID(1, true),
		PASSWORD(2, false),
		NAME(3, false),
		ADDRESS(4, false),
		PHONE(5, false);
		
		private int index;
		private boolean isPrimaryKey;
		
		private String[] columnNames = 	{
				"cid", 
				"password",
				"name",
				"address",
				"phone"};
		
		private Column(int index, boolean isPrimaryKey) 
		{
			this.index = index;
			this.isPrimaryKey = isPrimaryKey;
		}
		
		public int getIndex()
		{
			return index;
		}
		
		public boolean isPrimaryKey()
		{
			return isPrimaryKey;
		}
		
		public String toString()
		{
			return columnNames[this.ordinal()];
		}
	};
	
	public CustomerTable()
	{
		super(CUSTOMER_INSERT, CUSTOMER_DELETE, CUSTOMER_DISPLAY, EXISTS_QUERY);
	}
	
	public Customer login(String cid, String password) throws Exception
	{
		Customer customer = null;
		
		String fmtdQuery = String.format(LOGIN_QUERY, cid, password);
		ResultSet rs;
		rs = dbCon.executeQuery(fmtdQuery);
		
		if( rs != null )
		{	
			if(rs.next())
			{
				String name = 		rs.getString(Column.NAME.getIndex());
				String address = 	rs.getString(Column.ADDRESS.getIndex());
				String phone = 		rs.getString(Column.PHONE.getIndex());
									
				customer = new Customer(cid, password, name, address, phone);
			}
			
			rs.close();
		}
		else
		{
			throw new Exception("Invalid Customer ID / password combination");
		}
				
		return customer;
	}

	public void register(String cid, String password, String name, String address, String phone) throws SQLException
	{
		List<String> customerTuple = new ArrayList<String>(Column.values().length);
		customerTuple.add(Column.CID.ordinal(), cid);
		customerTuple.add(Column.PASSWORD.ordinal(), password);
		customerTuple.add(Column.NAME.ordinal(), name);
		customerTuple.add(Column.ADDRESS.ordinal(), address);
		customerTuple.add(Column.PHONE.ordinal(), phone);
		
		insert(customerTuple);		
	}

	@Override
	public List<String> getPrimaryKeyValues(List<String> insertValues) {
		List<String> primaryKeyValues = new ArrayList<String>();
		for(Column col : Column.values())
		{
			if(col.isPrimaryKey())
			{	
				primaryKeyValues.add(insertValues.get(col.ordinal()));
			}
		}
				
		
		return primaryKeyValues;
	}

	@Override
	public List<String> getColumnNames() {
		Column columns[] = Column.values();
		
		List<String> columnNames = new ArrayList<String>(columns.length);
		for( Column col : columns)
		{
			columnNames.add(col.ordinal(), col.toString());
		}
		return columnNames;
	}

	@Override
	public List<String> getPrimaryKeyColumnNames() {
		Column columns[] = Column.values();
		
		List<String> primaryKeyColumnNames = new ArrayList<String>(columns.length);
		for( Column col : columns)
		{
			if(col.isPrimaryKey())
			{
				primaryKeyColumnNames.add(col.ordinal(), col.toString());
			}
		}
		return primaryKeyColumnNames;
	}
}
